package com.hq.ims.userpc.controller;

import com.hq.authority.utils.AppUtils;
import com.hq.ims.data.annotation.UserOperationMonitor;
import com.hq.ims.data.service.BaseBusiChangeService;
import com.hq.ims.data.service.BaseBusiChangeTimeRecordService;
import com.hq.ims.data.service.BaseBusiHistoryService;
import com.hq.ims.data.service.BaseBusiInfoService;
import com.hq.ims.data.utils.LocalConstant;
import com.hq.ims.data.utils.WebExpandUtils;
import com.hq.ims.data.vo.BusiChangeMapstruct;
import com.hq.ims.data.vo.BusiChangeTimeRecordMapstruct;
import com.hq.ims.data.vo.BusiHistoryMapstruct;
import com.hq.ims.data.vo.BusiInfoMapstruct;
import com.hq.ims.userpc.busservice.UserBusiInfoService;
import com.hq.mybatis.base.page.RequestPage;
import com.hq.mybatis.base.page.ReturnPage;
import com.hq.utils.vo.ReturnVo;
import com.hq.web.annotations.GetJsonValue;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yinzhangxiao
 * @date 2024/10/29 13:31
 * @ClassName UserBusiInfoController
 **/
@Api(tags = "经营信息管理")
@RestController
@RequestMapping(LocalConstant.SYS_TYPE_USER_PC_URL_PREFIX + "/sys/busiInfo")
public class UserBusiInfoController {

    @Resource
    private UserBusiInfoService userBusiInfoService;
    @Resource
    private BaseBusiChangeTimeRecordService baseBusiChangeTimeRecordService;
    @Resource
    private BaseBusiHistoryService baseBusiHistoryService;
    @Resource
    private BaseBusiChangeService baseBusiChangeService;
    @Resource
    private BaseBusiInfoService baseBusiInfoService;


    @ApiOperation(value = "我的经营信息查询", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({})
    @PostMapping(value = "/getPage", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<ReturnPage> getPage(
            @RequestBody RequestPage page,
            @RequestBody BusiInfoMapstruct.PageSearchVo searchVo
    ) {
        searchVo.setPublisherId(AppUtils.getUserId());
        baseBusiInfoService.getByPage(page, searchVo);
        return ReturnVo.buildSuccess(page.getReturnPage());
    }

    @ApiOperation(value = "公司经营信息查询", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({})
    @PostMapping(value = "/getCompanyPage", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<ReturnPage> getCompanyPage(
            @RequestBody RequestPage page,
            @RequestBody BusiInfoMapstruct.PageSearchVo searchVo
    ) {
        searchVo.setOrgId(WebExpandUtils.getHeadCompanyId());
        baseBusiInfoService.getByPage(page, searchVo);
        return ReturnVo.buildSuccess(page.getReturnPage());
    }

    @ApiOperation(value = "产业链关联列表", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({})
    @PostMapping(value = "/professionRelevanceList", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<List<BusiInfoMapstruct.BusiInfoVo>> professionRelevanceList(
            @GetJsonValue(zh = "关键字",required = false) String keyWord
    ) {
        return ReturnVo.buildSuccess(userBusiInfoService.professionRelevanceList(keyWord));
    }

    @ApiOperation(value = "发布经营信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    @UserOperationMonitor
    @ApiImplicitParams({})
    @PostMapping(value = "/addData", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<Boolean> addData(@RequestBody BusiInfoMapstruct.SaveVo vo) {
        userBusiInfoService.addBusiInfo(vo);
        return ReturnVo.buildSuccess(true);
    }

    @ApiOperation(value = "撤销经营信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    @UserOperationMonitor
    @ApiImplicitParams({})
    @PostMapping(value = "/cancelData", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<Boolean> cancelData(@GetJsonValue(zh = "数据ID") String busiInfoId) {
        userBusiInfoService.cancelData(busiInfoId);
        return ReturnVo.buildSuccess(true);
    }

    @ApiOperation(value = "编辑经营信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    @UserOperationMonitor
    @ApiImplicitParams({})
    @PostMapping(value = "/updateData", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<Boolean> updateData(@RequestBody BusiInfoMapstruct.SaveVo vo) {
        userBusiInfoService.editBusiInfo(vo);
        return ReturnVo.buildSuccess(true);
    }

    @ApiOperation(value = "删除经营信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    @UserOperationMonitor
    @ApiImplicitParams({})
    @PostMapping(value = "/deleteData", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<Boolean> deleteData(@GetJsonValue(zh = "数据ID") String busiInfoId) {
        baseBusiInfoService.delByBusiInfoId(busiInfoId);
        return ReturnVo.buildSuccess(true);
    }

    @ApiOperation(value = "获取经营信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({})
    @PostMapping(value = "/getData", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<BusiInfoMapstruct.BusiInfoVo> getData(@GetJsonValue(zh = "数据ID") String busiInfoId) {
        return ReturnVo.buildSuccess(baseBusiInfoService.getDetailVo(busiInfoId));
    }


    @ApiOperation(value = "查看变更历史记录", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({})
    @PostMapping(value = "/getChangeData", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<List<BusiChangeTimeRecordMapstruct.BusiChangeTimeRecordVo>> getChangeData(
            @GetJsonValue(zh = "数据ID") String busiInfoId
    ) {
        return ReturnVo.buildSuccess(baseBusiChangeTimeRecordService.getChangeData(busiInfoId));
    }


    @ApiOperation(value = "查看经营历史信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({})
    @PostMapping(value = "/getHistoryData", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<BusiHistoryMapstruct.BusiHistoryVo> getHistoryData(
            @GetJsonValue(zh = "数据ID") String busiHistoryId
    ) {
        return ReturnVo.buildSuccess(baseBusiHistoryService.getDetailVo(busiHistoryId));
    }

    @ApiOperation(value = "查看源信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({})
    @PostMapping(value = "/getRootData", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<BusiInfoMapstruct.BusiInfoVo> getRootData(
            @GetJsonValue(zh = "数据ID") String parentId
    ) {
        return ReturnVo.buildSuccess(userBusiInfoService.getRootData(parentId));
    }

    @ApiOperation(value = "经营信息变更记录", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({})
    @PostMapping(value = "/getChangePage", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<ReturnPage> getChangePage(
            @RequestBody RequestPage page,
            @RequestBody BusiChangeMapstruct.PageSearchVo searchVo
    ) {
        if (StringUtils.isBlank(searchVo.getBusiInfoId())){
            throw new RuntimeException("业务参数错误！");
        }
        baseBusiChangeService.getByPage(page,searchVo);
        return ReturnVo.buildSuccess(page.getReturnPage());
    }

}
